Repository navigation
UI: the minimap's drawing at 98.3% (draft) - #470
kotijekriv wants to merge 11 commits into
Conversation
…index, two loads and the colour's push differ MinimapViewState::drawMinimap, the minimap: it copies the terrain picture from the minimap buffer to the screen (or to the loaded map's preview), draws every unit as a dot in its player's colour (with the wildlife hidden: own team and enemies in two colours), and draws the viewport's box. 946 instructions, rewritten from the original's listing, with gynt's draft beside it for the names: his best version is 10280b1 on his fork's reimpl/UI-Core-development ("32.8% | Reimplemented, asm differs"), his own commits for it f3cc8dd (23.32%) and 34548c6 (19.14%), on sourcehold#256 c899be1 and 95d388a, the newest e9bc49c on reimpl/UI-MenuItems-development (23.3%). Behaviour against the draft (every version): - `mapSize <= 200` where the draft has `< 200`: on a 200-tile map the draft reads the picture 100 columns and 100 rows away from where the original reads it; - the viewport box: the original returns where the draft has two empty ifs, so when the box sticks out on both sides it draws none (gynt has the returns himself in 8ca7861 on reimpl/Wilps-2026-10-18); - with the wildlife hidden, the original shows an enemy assassin up to and including distance 160; the draft only below 160 (mode 1's test is right in the draft); - the minimap buffer through `this` as in the original (the draft goes through the global instance; the same while `this` is that instance, and all five call sites pass their own `this`). What the match needed: the blit and the four pixel stores are handwritten assembly in the original (inline assembly here; it also brings the frame pointer); the loop variables of the two unit loops declared once at the function's level (declared per loop, their stack slots differ); the box position as two locals and the two offsets as the tile position; MC_MAP_SIZE_400 - mapSize written out each time; the first ten initialisations in the order found by a search over the orders; the colour switch in the original's block order. The numbers are main's map and screen-tile-table constants, as on sourcehold#465. Left (five instructions, found by a slot-blind comparison): the siege engines' colour index (2), where the original keeps a - 8 in the lea and its table base 0x00B39560, player 1's entry (every spelling tried folds it into the base); two loads swapped in front of the first unit loop's pixel loops (2); and the box's colour, pushed as -1 by the original and as 0xffff here (1): drawBorderBox's colour is `ushort` in the header, and `short` gives the original's push (gynt's open sourcehold#294 has drawBorderBox with `ushort`, so it is not changed here). Measured in a narrow build of this file on main 812d149: 97.6% raw, 90.1% normalised. Not run in game. Co-authored-by: Gynt <gynt@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Style guide reading (advisory, posted once) 2 possible deviation(s) from REIMPLEMENTATION_STYLE_GUIDE.md across 1 of the 1 reimplemented The match comes first. Where the ugly form is what matched, it stays -- that is the guide's own overriding rule, and rule 21 asks for a one-line comment saying what was measured so the next reader does not retry it. This report is posted once, when the PR opens, and is not repeated on later pushes.
WhereRule 8 — Let the compiler unroll
|
The style bot suggests letting MSVC unroll the two `offsetX += 2` loops. They draw every other pixel at half size, as in renderMinimapTileStripWithLuminescence (sourcehold#465), and get the same one-line comment. Written as a plain loop that skips the odd pixels, the function no longer matches: in a quick compile 378 lines differ instead of 10. Comments only; the listing is the same as before. Co-authored-by: Gynt <gynt@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A new generated-form typedef, UI::MiniMap::PlayerUnitColors = ushort[4][2]: per player the four animation frames' colours in the two colour modes. drawMinimap reads MiniMapDefinedData::PlayerColorColors through it, which declared the type inside the function until now (gynt on sourcehold#488: new types go in .hpp files, not in .cpp files). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The function-local typedef goes: the type is PlayerUnitColors from the header commit before this one. The file-local PICTURE_ROW_BYTES goes too: it is the same 400-byte row of a minimap buffer that main's MinimapConstants names MMC_BUFFER_ROW_BYTES, and the assembly's two uses take it through a using-declaration, since inline assembly cannot name a qualified constant. The function's listing is the same instruction for instruction. Co-authored-by: Gynt <gynt@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Feel free to make the header changes if it leads to improvements. Then I can address them in Ghidra. |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
drawMinimap passes drawBorderBox -1 for the viewport box's colour, and the original pushes it as a dword (0x004B727E push -1). With the header's ushort the call compiles to push 0xffff; with short it compiles to the original's push. The colour is a 16-bit pixel value either way, so the width stays. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A new generated-form enum for MinimapViewState::drawMinimap's flags argument: 1 centres the picture on the given offsets (0x004B6970 test 1), 2 draws the viewport's box (0x004B71FF test 2), 4 draws at half size (0x004B6814 test 4). The callers on main pass 5 and 7, so the parameter stays a uint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
drawMinimap reads the table at 0x1EC per colour slot (0 to 8), per animation frame (four) and per colour mode (two): [slot][0][mode] for most units (0x004B6CEA, 0x004B6D8F) and for the two colours with the wildlife hidden (0x004B6F01, 0x004B6F3D), [slot][frame][mode] for the siege engines (0x004B6D75), [0][2][mode] for the animals and the units without an owner (0x004B6C72, 0x004B6D51). Nine slots of 16 bytes end at 0x27C, where MinimapColorByLogicBit starts. The header had ushort[2][2] there and 68 single fields behind it (AnimalMinimapColour_RGB15/16 = [0][2], PlayerColorColors_entry2 = [1][0][0], field_0x1f8 to field_0x27a), all inside the table; none of them is used on main. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r type The three flag tests take MinimapDrawFlags, and the colour reads index MiniMapDefinedData::PlayerColorColors directly now that the header gives it its [9][4][2] shape, so the cast to PlayerUnitColors goes. The listing is the same as with the cast. With drawBorderBox's colour a short (the header commit before), the viewport box's colour is pushed as -1 as in the original (0x004B727E), and the two loads in front of the first unit loop's pixel loops come in the original's order (0x004B6DC5, widthFactor before heightFactor). The pair of loads at that loop's back edge (0x004B6E21, widthFactor before xPos in the original) is the other way round now. Narrow build: 98.3 raw and 91.6 normalised, from 97.6 and 90.1. Co-authored-by: Gynt <gynt@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MiniMapDefinedData::PlayerColorColors is ushort[9][4][2] now, so the per-slot typedef this branch added for drawMinimap's cast has no user left. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks! I made the header changes, each in a commit of its own:
I left two things for Ghidra, in the PR text. 0x004B6C20 and 0x004B7100 are loop heads inside drawMinimap, not functions; taking them out of the address list here moved a load tie in this function, so I left that to your sync. Also the names for field_0x34/0x38/0x44 and param_10. Four instructions are left: the siege engines' colour index (it still folds the -8 into the base whatever we write) and one pair of loads in a different order, which header changes keep moving around. |
One function at 98.3%, written from the original's listing with gynt's draft beside it for the names. This is a draft PR: four instructions still differ (below), so others can continue from here. Three header changes, each in a commit of its own; gynt allowed them here on 11-27 ("Feel free to make the header changes if it leads to improvements. Then I can address them in Ghidra."): drawBorderBox's colour is a
short,MiniMapDefinedData::PlayerColorColorsisushort[9][4][2], and a new enumMinimapDrawFlagsnames the flags. ThePlayerUnitColorstypedef from the last push goes again, since the field has the type now.MinimapViewState::drawMinimapgynt's best version is 10280b1 on his fork's reimpl/UI-Core-development ("32.8% | Reimplemented, asm differs"). His own commits for the draft are 34548c6 (19.14%) and f3cc8dd (23.32%), on #256 c899be1 and 95d388a, and the newest version is e9bc49c (2026-11-02, "reimplement: swap if/else arms across the UI view and modal folders") on reimpl/UI-MenuItems-development (23.3%).
Measured
Stronghold Crusader.exe(3bb0a8c1…5387ac5a), and sourcehold's reccmp with--resolve-wrapped-calls --ignore-security-cookie. The struct resolvers and the function resolvers are enabled locally (not committed).short(81451bc) 98.3 / 91.6, the flags enum, the colour table and the code using both (eac9a8b) 98.3 / 91.6 with the same listing, the typedef's removal (9187994, the head) 98.3 / 91.6.renderDebugDataCurrentPlayerData,TransformAndRenderPercentage): 0 failed of 2,906 and 2,907 compile steps;drawMinimap98.3 / 91.6 there, the same as in its narrow build; every main function reads what it read before (2,922 functions, average 99.54 in both builds), apart fromMenu::handleMenuItemsandMenu::Constructor_Menu, which is the 0x11000000 printing reading described in UI: the minimap's second picture at 100% #465. All 21 files on main that call drawBorderBox keep their numbers with theshortcolour.lea ecx, [ecx]at 0x004B6C1D, 0x004B6DCD and 0x004B6F1D,lea esp, [esp]at 0x004B6F59 and 0x004B70F7,mov edi, ediat 0x004B6DEE and 0x004B70FE, and two jumps over them). Compared with the stack slots' numbers taken out, four instructions differ (below); every one of the 322 stack slot uses is on the original's slot.What the function does
It draws the minimap. First it redraws the minimap's own pictures when the map's orientation changed, and runs the step-by-step redraw that
field_0x38/field_0x34drive (2% of the rows per call, withcollectCliffEdgeTilesForClimbDataat the start of each pass). Then it works out which part of the picture (400 rows of 200 pixels) shows: centred on the given offsets when flag 1 is set, and clamped to the map. It copies that part to the pencil surface, or to the loaded map's preview when the last argument is above 0. It draws every unit on a visible tile as a dot in its player's colour; siege engines take the current animation frame, animals a colour of their own, and enemy assassins out of sight are not drawn. With the wildlife hidden (Alt+R), only units with an owner are drawn, own team and enemies in two colours. Finally, with flag 2, it draws the viewport's box.Behaviour against the draft
Read block by block against every version of gynt's draft:
mapSize <= 200(0x004B6827cmp ..., 200; jg). Every version of the draft has< 200, so on a 200 by 200 map it read the picture 100 columns and 100 rows away from where the original reads it. The same slip as inrenderMinimapTileColors(UI: the minimap's terrain colours at 100% #461) andrenderMinimapTileStripWithLuminescence(UI: the minimap's second picture at 100% #465).jgeto the epilogue). 10280b1 and e9bc49c have two emptyif (...) {}there and draw a clamped box. gynt has both returns himself in 8ca7861 on his reimpl/Wilps-2026-10-18.jle). Every version of the draft draws one only below 160. In the other mode the draft's test is right.this(0x004B66E8lea edi, [esi + 0x44]), where the draft goes through the global instance. These are the same whilethisis that instance, and all five call sites (0x004B73A9, 0x004B74F9, 0x004B75D0, 0x004B76A7, 0x004B76D0) pass their ownthison.Not the draft's mistake: the screen tile table indexed
- 10/- 8, which was right for the header of its day.What got it to 98.3%
renderMinimapPreviewanddrawCurrentPixelon main. The blit is 30 instructions (0x004B6B1D to 0x004B6B79) and each pixel store 10 or 12: the original's handwritten instructions and nothing else, every operand a C++ local. Nothing of them can move to C++: they build the address withmov edx, ...; mul edxandadd eax, eax, where the compiler writesimulandlea. The blit's block (it has labels) also brings the frame pointer; the draft forced it with#pragma optimize("y", off). The pixel stores come in two forms, each used twice; with two forms and two uses each they stay blocks here rather than a macro (a macro header undersrc/OpenSHCwould also trip the declaration check).widthandheightcount tiles from the point where they are divided, as in the original, which keeps the counts in their stack slots.MC_MAP_SIZE_400 - mapSizeis written out each time (a local for it changes the stack slots).shortin the header: the box's colour is pushed as -1 as in the original (0x004B727E), and the two loads in front of the first unit loop's pixel loops come in the original's order with it (0x004B6DC5,widthFactorbeforeheightFactor).VC_SCREEN_POINT_*), the map sizes and the 400 rows (MC_MAP_*), the small map's margin(MC_MAP_XY_LIMIT - MC_MAP_SIZE_200) / 2, the blit's row in bytes as main'sMMC_BUFFER_ROW_BYTESthrough a using-declaration, because inline assembly cannot name a qualified constant, and the flags asMinimapDrawFlags.What is left (four instructions)
- 8in the index and its table base at 0x00B39560 (lea eax, [edx + ecx*2 - 8], then[eax*2 + 0xb39560]). About twenty spellings tried, all of which fold the constant into the base (0x00B39550): flat and 2-D indexes, the table taken from either field, signed and unsigned operands, the index in a variable of its own, a__forceinlinehelper, scoped bound locals in both orders, a per-player struct. With the table typedushort[9][4][2]now, eight more fold it the same way: the field indexed directly, the table split at player 1 and indexed[slot - 1], the base taken at[1]with the- 8inside the index instead of on top, flat and two-dimensional.widthFactorbeforexPosbehind the pixel loop, ours the other way round. This is a tie that header changes move. With drawBorderBox's colour asushortit was the pair in front of the same loop's pixel loops (0x004B6DC5,widthFactorbeforeheightFactor) and this one matched; withshortit is the other way round; taking the two loop heads out of the address list (see the header findings) swapped a third pair as well (0x004B70CF, in the second loop). One pair is off in every state tried. Before: about a dozen variants (the bound test as separatecontinues in one or both loops, nested ifs, the dot's size as scope locals in both orders, a helper-shaped scope); now five more on the back edge (operands of the bound test turned round, the bound's product turned round,baseYbeforebaseX): ties or worse.For whoever continues:
MiniMapDefinedData + 0x1FC: player 1's entry,PlayerColorColors[1](the old header'sPlayerColorColors_entry2), and the- 8is one player's eight words back. Only this arm has it; it is also the only arm with the frame, and it readsthisback from its spill slot (0x004B6D5E). The other player-colour reads (0x004B6CEA, 0x004B6D8F, 0x004B6F01, 0x004B6F3D) use 0x00B39550 with no displacement (the animals' two 0x00B39558). So the original's source indexed this one table from player 1.currentPlayerSlotIDinediaround the draw code (loaded at 0x004B6EEE, reloaded at 0x004B71C4); in the first loopediholds nothing there. So the two loops differ in their register state in front of the block, not in the block's own code. The original loadswidthFactorfirst at both places of the first loop; ours puts it betweenheightFactorandxPos, before the one and after the other, and which way round depends on the headers.Things in the file that look odd and are the original's
if (mapSize == MC_MAP_SIZE_160) {} else if (mapSize == MC_MAP_SIZE_200) xPos -= 2;behind the blit: the original tests 160 first (0x004B6B81). A 160-tile map keeps its 3-pixel shift and a 200-tile map one of its three.(int)this->field_0x34 >= 100and the like: the original compares signed, and the header hasundefined4.Checker CHECKs (not changed)
field_0x34,field_0x38andfield_0x44; gynt's headers on reimpl/Wilps-2026-10-18 (8ca7861) call themincrementalRedrawPercent,pendingRedrawPassesandcompositedPixelBuffer.MiniMapDefinedData.hppreplaced by one array field: the offsets are checked (0x1EC + 144 = 0x27C), and nothing on main or in an open PR uses the removed names.Header findings
Changed here, each in a commit of its own; gynt allowed them here on 11-27 ("Feel free to make the header changes if it leads to improvements. Then I can address them in Ghidra."):
short(81451bc:PencilRenderCore.hppand the resolver inPencilRenderCore.func.hpp). The original passes it -1 as a dword (0x004B727Epush -1); withushortthe call compiles topush 0xffff. Withshortthis function goes from 97.6 to 98.3: the push matches, and so do the two loads in front of the first unit loop's pixel loops. All 21 files on main that call it keep their numbers (the full build above); in the earlier pair of full builds, withint, all 19 callers then on main read differently, so the callers fit a 16-bit parameter and this function a signed one. gynt's open Reimplement 17 UI rendering, minimap and credits functions #294 implements drawBorderBox withushort color: whichever of the two lands second has to bring its.cppsignature or this header line along.MiniMapDefinedData::PlayerColorColorsisushort[9][4][2](33693e8): per colour slot (0 to 8) four animation frames in the two colour modes, 0x1EC to 0x27C, whereMinimapColorByLogicBitstarts. This function reads it at[slot][0][mode](0x004B6CEA, 0x004B6D8F, 0x004B6F01, 0x004B6F3D),[slot][frame][mode](0x004B6D75) and[0][2][mode](0x004B6C72, 0x004B6D51). The header hadushort[2][2]there and 68 single fields behind it (AnimalMinimapColour_RGB15/16=[0][2],PlayerColorColors_entry2=[1][0][0],field_0x1f8tofield_0x27a), all inside the table; none of them is used on main or in an open PR. The function indexes the field directly now, the same listing as with the cast, and thePlayerUnitColorstypedef this PR added on 11-25 goes again (9187994).UI/Enums/MinimapDrawFlags.hpp(66a6efe), in the generated form:MDF_CENTER_ON_OFFSETS= 1 (0x004B6970test 1),MDF_VIEWPORT_BOX= 2 (0x004B71FFtest 2),MDF_HALF_SIZE= 4 (0x004B6814test 4). The callers on main pass 5 and 7, so the parameter stays auint. The same listing.Proposed, not changed here:
type: functionwithout a label insrc/precomp/addresses-SHC-3BB0A8C1.hppand asPendingin the status list, but both lie inside this function (0x004B66C0 to 0x004B729C), each behind alignment filler: the first unit loop's row head (reached from 0x004B6C1Bjmpand 0x004B6ECDjl) and the second's (0x004B70F5jmp, 0x004B712Fjl). Nothing in the image calls them or holds their address. Taking the two entries out here was measured: every file includes that header, and the edit alone swapped a third pair of loads in this function (98.3 to 98.2). So it is left to Ghidra; the sync that drops them may move this function's load tie the same way.MinimapViewState::field_0x34/field_0x38are the step-by-step redraw's strip start (in percent, stepped by 2) and its passes left, andfield_0x44is the picture (400 rows of 200 pixels) this function copies from; gynt's headers on reimpl/Wilps-2026-10-18 (8ca7861) call themincrementalRedrawPercent,pendingRedrawPassesandcompositedPixelBuffer. The last parameter (param_10in the header) isintoPreviewhere: above 0 the picture goes intoloadedMiniMapinstead of the screen.🤖 Generated with Claude Code